package com.share.common.logback;


import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.share.common.utils.ApplicationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * 在spring容器初始化完成时会调用，InitializingBean.afterPropertiesSet()，再调用init-method()
 * logback日志转换器
 */
@Slf4j
public class PortConverter extends ClassicConverter implements ApplicationListener<ContextRefreshedEvent> {
    private static String port = "";

    @Override
    public String convert(ILoggingEvent event) {
        return port;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        port = ApplicationUtil.getEnvironmentProperty("server.port");
    }
}

